package kassiopeia.agent.repository;

import kassiopeia.commons.model.Coordinates;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;

@Component
@ManagedResource
public class RepositoryManagerMBean {

	@Autowired
	private RepositoryManager repositoryManager;

	@ManagedOperation
	public String getRemoteRepositoryUrl() {
		return repositoryManager.getRemoteRepositoryUrl();
	}

	@ManagedOperation
	public String getLocalRepositoryUrl() {
		return repositoryManager.getLocalRepositoryUrl();
	}

	@ManagedOperation(
		description="Checks if plugin with given coordinates is present in local repository")
	@ManagedOperationParameters({
		@ManagedOperationParameter(name="groupId", description="Group Identifier"),
		@ManagedOperationParameter(name="pluginId", description="Plugin Identifier"),
		@ManagedOperationParameter(name="version", description="Plugin version")
	})
	public boolean searchInLocalRepo(String groupId, String pluginId, String version) {
		Coordinates coordinates = new Coordinates(groupId, pluginId, version);
		if(repositoryManager.searchLocalRepo(coordinates) != null) {
			return true;
		}
		return false;
	}

	@ManagedOperation(
			description="Checks if plugin with given coordinates is present in remote repository")
	@ManagedOperationParameters({
		@ManagedOperationParameter(name="groupId", description="Group Identifier"),
		@ManagedOperationParameter(name="pluginId", description="Plugin Identifier"),
		@ManagedOperationParameter(name="version", description="Plugin version")
	})
	public boolean searchInRemoteRepo(String groupId, String pluginId, String version) {
		Coordinates coordinates = new Coordinates(groupId, pluginId, version);
		if(repositoryManager.searchRemoteRepo(coordinates) != null) {
			return true;
		}
		return false;
	}
}
